﻿namespace Rules
{
    public class StringLengthRule : RuleBase<string>
    {
        public StringLengthRule(int minLength, int maxLength, string propertyName)
            : base(propertyName)
        {
            MinLength = minLength;
            MaxLength = maxLength;
        }

        public StringLengthRule(int minLength, int maxLength, string propertyName, string description)
            : base(propertyName, description)
        {
            MinLength = minLength;
            MaxLength = maxLength;
        }

        public int MinLength { get; private set; }
        public int MaxLength { get; private set; }

        protected override bool DoEvaluate(string subject)
        {
            if (string.IsNullOrEmpty(subject))
                return true;
            return subject.Length >= MinLength && subject.Length <= MaxLength;
        }

        protected override string GetDefaultDescription()
        {
            return string.Format("{0} must be between {1} and {2} characters long", PropertyName, MinLength, MaxLength);
        }
    }
}